<?php
/**
 * 轻舟CMS(QZLOG)
 *
 * 一款基于PHP+MYSQL的开源内容管理系统，模板标签简单、可扩展性强，利于二次开发，轻松建站的首选利器。
 *
 * @package     QZLOG
 * @license     http://www.qzlog.com/license
 * @link        http://www.qzlog.com
 * @mail        info@shyohan.com
 * @since       Version 3.0
 * @copyright   Copyright (c) 2015 - 2017 上海优翰信息科技有限公司. All rights reserved.
 */

defined('IN_QZLOG') OR exit('QZLOG:No direct script access allowed!');

/**
 * 数据库备份控制器
 * Class BackupControl
 * @author LINK <link@shyohan.com>
 */
class BackupController extends QzController
{

    /**
     * 备份列表
     * @author LINK <link@shyohan.com>
     */
    public function index()
    {
        $file = Dir::tree(TEMP_PATH . "Backup");
        $dir = array();
        foreach ($file as $f) {
            if (is_dir($f['path'])) {
                $dir[] = $f;
            }
        }
        $bread = make_bread(array('数据管理' => '', '备份列表' => ''));
        $this->assign("dir", $dir);
        $this->assign('bread', $bread);
        $this->display();
    }

    /**
     * 数据备份
     */
    public function backup_db()
    {
        $result = Backup::backup(
            array(
                'size' =>Q('post.size'),//分卷大小
                'structure' => false,//表结构
                'dir' => TEMP_PATH . 'Backup/' . date("Ymdhis") . '/',//备份目录
                'step_time' => 1,//备份时间间隔
            )
        );
        if ($result === false) {
            //备份发生错误
            $this->error(Backup::$error, U('index'));
        } else {
            if ($result['status'] == 'success') {
                //备份完成
                $this->success($result['message'], U('index'));
            } else {
                //备份过程中
                $this->success($result['message'], $result['url'], 0.2);
            }
        }
    }

    /**
     * 配置数据备份
     */
    public function backup()
    {
        $bread = make_bread(array('数据管理' => '', '数据备份' => ''));
        $this->assign('bread', $bread);
        $this->assign("table", M()->getAllTableInfo());
        $this->display();
    }

    /**
     * 还原数据
     */
    public function recovery()
    {
        $dir = TEMP_PATH . "Backup/" . Q("dir");
        $result = Backup::recovery(array('dir' => $dir));
        if ($result === false) {//还原发生错误
            $this->error(Backup::$error, U('index'));
        } else {
            if ($result['status'] == 'success') {//还原完毕
                $this->success($result['message'], U('index'));
            } else {//还原运行中...
                $this->success($result['message'], $result['url'], 0.2);
            }
        }
    }

    /**
     * 优化表
     */
    public function optimize()
    {
        if (!empty($_POST['table'])) {
            foreach ($_POST['table'] as $t) {
                M()->optimize($t);
            }
            qz_ajax_return('优化表成功');
        }
    }

    /**
     * 修复表
     */
    public function repair()
    {
        if (!empty($_POST['table'])) {
            foreach ($_POST['table'] as $t) {
                M()->repair($t);
            }
            qz_ajax_return('修复表成功');
        }

    }

    /**
     * 删除备份目录
     */
    public function del()
    {
        $dir = Q('id', '', 'trim');
        if (Dir::del(TEMP_PATH . 'Backup/' . $dir)) {
            qz_ajax_return('删除成功');
        } else {
            qz_ajax_return_error('删除失败');
        }

    }

}